package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn00161Bo;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferProcessHistory;

public class TransferSeniorSearchRebackController extends TmsController {

	private Logger logger = Logger
			.getLogger(TransferSeniorSearchRebackController.class);

	@Override
		public void execute(UserContext uc) throws Exception {
			logger.debug(this.getClass().getName() + "#execute was called.");
			TxController controller = getTxController();
			try {
				controller.begin();
				String serial = (String)uc.getAttribute("serial");
				TmsUserProfile profile = uc.getUserProfile();
				TransferAcceptedInfo accept = new TransferAcceptedInfo();
				accept = accept.getAcceptInfoBySerial(serial, "");
				if(accept!=null)
				{
					//进行业务重置
					String newSserial = "CB"+serial;//geng xin
					//根据ID更新
					TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
					accept1.setSerial(newSserial);
					accept1.setStatus(16);
					accept1.setId(accept.getId());
					accept1.setUpdateUserId(profile.getUniqueId());
					accept1.updateTransferInfoAtReback(accept1);
										
					//根据主键ID更新
					TransferFinanceInfo finance1 = new TransferFinanceInfo();
					finance1 = finance1.findFinanceInfoByserial(serial);
					finance1.setSerial(newSserial);
					finance1.updateFinanceInfoAtReback(finance1);
					
					//开始向库存表中添加信息
					accept.setInsertUserid(profile.getUniqueId());
					accept.setUpdateUserId(profile.getUniqueId());
					accept.setStatus(3);
					accept.insertTransInfoAtBeBack(accept);
					
					//财务表添加
					TransferFinanceInfo finance = new TransferFinanceInfo();
					finance.insertFinanceByInner(serial);
					
					//历史记录
					TransferProcessHistory history = new TransferProcessHistory();
					history.setSerial(serial);
					history.setBusinessType(40);
					history.setInsertUserId(profile.getUniqueId());
					history.setReason("");
					history.insertHisInfoAtAssess(history);
					
					uc.setAttribute("tishixinxi", "3");
				}
				// add by huangheli 2016/01/09 start
				MobileMessage message = new MobileMessage();
				message.putValue("Serial", serial);
				message.setMessage("过户同步[高级处理 - 业务重办]接口,过户流水号为#Serial#过户信息不存在，无法同步TMS的高级处理（业务重办）");
				TransferDataSyn00161Bo bo = new TransferDataSyn00161Bo();
				bo.setMobileMessage(message);
				bo.setSerial(serial);
				bo.setFunc(14);
				bo.setStatus(2);
				bo.setDetailStatus(40);
				PullingExecutor.getInstance().submit(bo);	
				// add by huangheli 2016/01/09 end
				controller.commit();
			} catch (Exception e) {
				try {
					controller.rollback();
				} catch (TransactionException e1) {
					throw new TmsException(TmsException.DB_TXCRROR, e1);
				}
				// 系统异常
				if (e instanceof TmsException) {
					throw e;
				}
				throw new TmsException(TmsException.DB_TXCRROR, e);
			}
			logger.debug(this.getClass().getName() + "#execute was completed.");

		}
}
